using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using DG.Tweening;
using UnityEngine.UI;
using Unity.VisualScripting;
namespace NuoYan.Extension
{
    public class RotateButton : ButtonEx
    {

        public  enum RotateMode
        {
            /// <summary>
            /// 不转
            /// </summary>
            None,
            /// <summary>
            /// 交互时转
            /// </summary>
            Interactable,
            /// <summary>
            /// 只有鼠标进入或者离开时转
            /// </summary>
            Only,
            /// <summary>
            /// 一直转
            /// </summary>
            Always
        }
        [Tooltip("按钮旋转模式")]
        public RotateMode rotateMode = RotateMode.Always;
        [Tooltip("是否随机旋转轴")]
        public bool _RandomAxis = false;
        private Quaternion originRotation;

        [Tooltip("旋转轴")]
        [SerializeField] Vector3 rotateAxis;
        [Range(-10, 10)]
        [Tooltip("旋转速度")][SerializeField] float rotateSpeed = 1;
        [Tooltip("随机旋转时间间隔")][Range(1, 10)]
        public float randomTime = 2f;
        private float timer = 0;
        private Vector3 angle;
        private CanvasGroup canvasGroup;
        protected override void Awake() 
        {
            base.Awake();
            originRotation = transform.localRotation; 
            canvasGroup = gameObject.GetOrAddComponent<CanvasGroup>();
        }
 
        protected override void Update()
        {
            base.Update();
            SwitchRotateMode();
            RandomAxis(_RandomAxis);
        }
        private void SwitchRotateMode()
        {
            switch (rotateMode)
            {
                case RotateMode.None:
                    transform.DORotate(originRotation.eulerAngles, 0.5f);
                    rotateAxis = new Vector3(0, 0,1);
                    break;
                case RotateMode.Interactable:
                    if (wasHovered)
                    {
                        Turn();
                    }
                    else
                    {
                        transform.DORotate(Vector3.zero, 0.5f);
                    }
                    break;
                case RotateMode.Always:
                    Turn();
                    break;
                case RotateMode.Only:
                    if (wasHovered)
                    {
                        transform.DORotate(new Vector3(0,0,Random.Range(-360, 360)), 0.5f);
                    }
                    break;
                default:
                    transform.DORotate(Vector3.zero, 0.5f);
                    break;
            }
        }


        public void Turn()
        {
            float angleIncrement = -rotateSpeed * 100 * Time.deltaTime;
            float maxAxisValue = Mathf.Max(rotateAxis.x, rotateAxis.y, rotateAxis.z);

            if (rotateAxis.x > 0 && rotateAxis.x == maxAxisValue && rotateAxis.x > rotateAxis.y && rotateAxis.x > rotateAxis.z)
            {
                angle.x += angleIncrement;
            }
            else if (rotateAxis.y > 0 && rotateAxis.y == maxAxisValue &&  rotateAxis.y > rotateAxis.x && rotateAxis.y > rotateAxis.z)
            {
                angle.y += angleIncrement;
            }
            else if (rotateAxis.z > 0 && rotateAxis.z == maxAxisValue && rotateAxis.z > rotateAxis.x && rotateAxis.z > rotateAxis.y)
            {
                angle.z += angleIncrement;
            }
            else
            {
                angle.z += angleIncrement; //默认旋转z轴
            }

            transform.DORotate(angle, 0.5f);

            angle.x = Mathf.Repeat(angle.x, 360);
            angle.y = Mathf.Repeat(angle.y, 360);
            angle.z = Mathf.Repeat(angle.z, 360);
        }

        public void RandomAxis(bool isRandom)
        {
            if(isRandom)
            {
                 if(timer >= 0)
                {
                    timer -= Time.deltaTime;
                }
                else
                {
                    timer = randomTime;
                    rotateAxis = new Vector3(Random.Range(0, 100), Random.Range(0, 100), Random.Range(0, 100));
                }
            }
            
        }

    }
}
